// pages/index3/index3.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cityName: '',
    city_YQ1: {}
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let cityName = options.name
    this.getCityName(cityName)
    this.getYiQing(cityName)
  },

  getCityName (e) {
    let that = this
    that.setData({
      cityName: e
    })
  },


  //获取地区疫情情况
  getYiQing (c) {
    let that = this;
    // console.log(c)
    wx.request({
      url: 'https://view.inews.qq.com/g2/getOnsInfo',
      data: {
        "name": "disease_h5",
        "callback": "jQuery3510005159808073531336_1633096619508",
        "_": "1633096619509"
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        var yqdata = res.data.match(/\((.+)\)/g);//正则表达式（提取括号中的字符串）
        var jsondata = yqdata[0].replace('(', '')
        var _data = jsondata.replace(')', '')
        var data = JSON.parse(_data).data
        var obj_data = JSON.parse(data);
        // console.log(obj_data.areaTree[0].children)
        that.setData({
          city_YQ: {
            lastUpdateTime: obj_data.lastUpdateTime,
            chinaTotal: {
              children: [obj_data.areaTree[0].children]
            }
          }
        })
        // console.log(that.data.city_YQ.chinaTotal.children)
        //调用方法（遍历省份数据提取当前地区的疫情情况）
        let CityYiQinig = that.data.city_YQ.chinaTotal.children
        that.getCityYiQing(c, CityYiQinig);

      },
      fail: function () {
        // fail
        wx.showModal({
          title: '提示',
          content: "疫情数据获取失败？",
          success: function (res) {
            if (res.confirm) {
              console.log(res.confirm, '用户点击确定')
              //getYiQing(c);//重新调用获取疫情数据方法
            }
          }
        })
      },
      complete: function () {
        // complete
      }
    })
  },


  //遍历省份数据提取当前地区的疫情情况
  getCityYiQing (c, e) {
    let that = this;
    let cityyq = c
    console.log(cityyq, "地区")
    for (let i = 0; i < e[0].length; i++) {
      for (let j = 0; j < e[0][i].children.length; j++) {
        // console.log(e[0][i].children[j].name)
        if (cityyq == e[0][i].children[j].name) {
          console.log(e[0][i].children[j])
          that.setData({
            city_YQ1: e[0][i].children[j]
          })
        }
      }

      if (cityyq == e[0][i].name) {
        console.log(e[0][i], "省")
        that.setData({
          city_YQ1: e[0][i]
        })
        // console.log(that.data.city_YQ1)
      }
    }
  },


  getYiQing_Q (e) {
    console.log(e.currentTarget.dataset)
    let that = this;
    let name = e.currentTarget.dataset.name
    wx.navigateTo({
      url: '/pages/index3/index3?&name=' + name,
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
        wx.showModal({
          title: '提示',
          content: `${{ name }}市的疫情数据请求失败！`,
          success: (res) => {
            if (res.confirm) {
              console.log(res.confirm, '用户点击确定')
            }
          }
        })
      },
      complete: function () {
        // complete
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})